require(['jquery','token','common/data','sweetalert','libs/vue-validate','bootstrap'], function($,token,syncData,swal) {
    var content=new Vue({
        el: '.content',
        data:syncData.fetch(),
        filters:{
            currency: {
                read: function (val) {
                    val=+val;
                    return val.toFixed(2)
                },
                // write 函数会在数据写入到模型之前被调用
                write: function (val, oldVal) {
                    var number = +val.replace(/[^\d.]/g, '')
                    return isNaN(number) ? 0 : parseFloat(number.toFixed(2))
                }
            }
        },
        methods:{
            completeOrder:function(id){
                $.ajax({
                    url:'/api/order',
                    method:'delete',
                    data:{
                        _id:id,
                    },
                    success:function(data){
                        console.log(data)
                        if(data.status=="success"){
                            window.location.reload();
                        }
                    }
                })
            }
        }
    })
    var modal=new Vue({
        el: '#myModal',
        validators: {
            isPhone: function (val/*,rule*/) {
                return /^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/.test(val)
            }
        },
        filters:{
           currency: {
               read: function (val) {
                   return val.toFixed(2)
               },
               // write 函数会在数据写入到模型之前被调用
               write: function (val, oldVal) {
                   var number = +val.replace(/[^\d.]/g, '')
                   return isNaN(number) ? 0 : parseFloat(number.toFixed(2))
               }
           }
        },
        data:{
            rule:{
                dish_name:{
                    required: { rule: true, message: '请输入商品名 !!' },
                },
                dish_desc:{
                    required: { rule: true, message: '请输入商品描述 !!' },
                },
                dish_price:{
                    required: { rule: true, message: '请输入起送金额 !!' }
                }
            },
            dish_price:0,
        },
        methods: {
            submit: function(){
                if(this.$validation1.invalid){
                    swal("Oops...", this.$validation1.errors[this.$validation1.errors.length-1].message, "error");
                }else {
                    var dish_name=$('#dish_name').val();
                    var dish_desc=$('#dish_desc').val();
                    var dish_price=$('#dish_price').val();
                    $.ajax({
                        url:'/api/dish',
                        method:'post',
                        data:{
                            name:dish_name,
                            desc:dish_desc,
                            price:dish_price,
                        },
                        success:function(data){
                            console.log(data)
                            if(data.status=="success"){
                                window.location.reload();
                            }
                        }
                    })
                }
            }
        }
    })

});